import sqlite3
class EastmoneyPipeline(object):
    con=None
    cursor=None


    def open_spider(self,spider):
        self.con=sqlite3.connect("eastmoney.db")
        self.cursor=self.con.cursor()
        self.cursor.execute("create table money("
                            "id int,"
                            "stocksymbol varchar(16),"
                            "stockname varchar(16),"
                            "LatestPrice varchar(16),"
                            "Pricelimit varchar(16),"
                            "Riseandfall varchar(16),"
                            "volume varchar(16),"
                            "turnover varchar(16),"
                            "amplitude varchar(16),"
                            "max varchar(16),"
                            "min varchar(16),"
                            "today varchar(16),"
                            "yesterday varchar(16))")


    def process_item(self, item, spider):
        try:
            data = item['data']
            num = item['num']
            self.cursor.execute("insert into money values(?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",(num,data['f12'],data['f14'],data['f2'],data['f3'],data['f4'],data['f5'],data['f6'],data['f7'],data['f15'],data['f16'],data['f17'],data['f18']))
            print("Successfully inserted")
            self.con.commit()
        except Exception as e:
            print(e)
            self.con.rollback()
    def close_spider(self,spider):
        self.cursor.close()
        self.con.close()

